package com.customview;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2021/3/25 15:29
 */

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.OvershootInterpolator;

import com.customview.R;

import java.awt.font.TextAttribute;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



public class ScoreCircleView extends View {

    private Paint mBackPaint, mProgPaint,mTextPaint;   // 绘制画笔
    private RectF mRectF;       // 绘制区域
    private int[] mColorArray;  // 圆环渐变色
    public float mTextWidth;
    public float mTextHeight;

    public int POneColor;
    public int PTwoColor;
    public int PThreeColor;
    public ScoreCircleView(Context context) {
        this(context, null);
    }

    public ScoreCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScoreCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        @SuppressLint("Recycle")
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScoreCircleView);

        POneColor=typedArray.getColor(R.styleable.ScoreCircleView_sprogOneColor, Color.BLUE);
        PTwoColor=typedArray.getColor(R.styleable.ScoreCircleView_sprogTwoColor, Color.BLUE);
        PThreeColor=typedArray.getColor(R.styleable.ScoreCircleView_sprogThreeColor, Color.BLUE);
        // 初始化背景圆环画笔
        mBackPaint = new Paint();
        mBackPaint.setStyle(Paint.Style.STROKE);    // 只描边，不填充
        mBackPaint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        mBackPaint.setAntiAlias(true);              // 设置抗锯齿
        mBackPaint.setDither(true);                 // 设置抖动
        mBackPaint.setStrokeWidth(typedArray.getDimension(R.styleable.ScoreCircleView_sbackWidth, 5));
        mBackPaint.setColor(typedArray.getColor(R.styleable.ScoreCircleView_sbackColor, Color.LTGRAY));

        // 初始化进度圆环画笔
        mProgPaint = new Paint();
        mProgPaint.setStyle(Paint.Style.STROKE);    // 只描边，不填充
        mProgPaint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        mProgPaint.setAntiAlias(true);              // 设置抗锯齿
        mProgPaint.setDither(true);                 // 设置抖动
        mProgPaint.setStrokeWidth(typedArray.getDimension(R.styleable.ScoreCircleView_sprogWidth, 10));

        //初始化文本
        mTextPaint=new Paint();
        mTextPaint.setTextAlign(Paint.Align.RIGHT);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setAntiAlias(true);            // 设置抗锯齿
        mTextPaint.setDither(true);                 // 设置抖动
        mTextPaint.setTextSize(typedArray.getDimension(R.styleable.ScoreCircleView_sprogTextSize,16));
        mTextPaint.setColor(typedArray.getColor(R.styleable.ScoreCircleView_sprogTextColor, Color.BLACK));



        // 初始化进度圆环渐变色
        int startColor = typedArray.getColor(R.styleable.ScoreCircleView_sprogStartColor, -1);
        int firstColor = typedArray.getColor(R.styleable.ScoreCircleView_sprogFirstColor, -1);
        if (startColor != -1 && firstColor != -1) mColorArray = new int[]{startColor, firstColor};
        else mColorArray = null;


        typedArray.recycle();

        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int viewWide = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        int viewHigh = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        int mRectLength = (int) ((viewWide > viewHigh ? viewHigh : viewWide) - (mBackPaint.getStrokeWidth() > mProgPaint.getStrokeWidth() ? mBackPaint.getStrokeWidth() : mProgPaint.getStrokeWidth()));
        int mRectL = getPaddingLeft() + (viewWide - mRectLength) / 2;
        int mRectT = getPaddingTop() + (viewHigh - mRectLength) / 2;
        mRectF = new RectF(mRectL, mRectT, mRectL + mRectLength, mRectT + mRectLength);

//        // 设置进度圆环渐变色
//        if (mColorArray != null && mColorArray.length > 1)
//            mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR));
//
    }

    int mStrokWidth=dp2px(30);
    int mTextMargin=dp2px(18);
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(mRectF, 270,270 , false, mBackPaint);
        canvas.drawArc(mRectF.left+mStrokWidth,mRectF.top+mStrokWidth,mRectF.right-mStrokWidth,mRectF.bottom-mStrokWidth,270,270,false,mBackPaint);
        canvas.drawArc(mRectF.left+mStrokWidth*2,mRectF.top+mStrokWidth*2,mRectF.right-mStrokWidth*2,mRectF.bottom-mStrokWidth*2,270,270,false,mBackPaint);

        for (int i = 0; i < list.size(); i++) {
            if (i==0){
                mProgPaint.setColor(getProgressColor(i));
                canvas.drawArc(mRectF, 270, 270 *presont*list.get(i).getValues() / 100, false, mProgPaint);

            }else
            {
                mProgPaint.setColor(getProgressColor(i));

                canvas.drawArc(mRectF.left+mStrokWidth*i,mRectF.top+mStrokWidth*i,mRectF.right-mStrokWidth*i,mRectF.bottom-mStrokWidth*i,270,270 *presont*list.get(i).getValues() / 100,false,mProgPaint);

            }

        }

        for (int i = 0; i < list.size(); i++) {
            Rect rect = new Rect();
            mTextPaint.getTextBounds(list.get(i).getName(),0,list.get(i).getName().length(),rect);
            int textHeight = rect.height()/4;
            canvas.drawText(list.get(i).getName(),getMeasuredWidth()/2-mTextMargin,mRectF.top+mStrokWidth*i+textHeight,mTextPaint);

        }
    }



    /**
     * 初始化界面
     */
    public void init() {

        ValueItem item1=new ValueItem("力度",00);
        ValueItem item2=new ValueItem("时长",00);
        ValueItem item3=new ValueItem("区域",00);
        ArrayList<ValueItem> objects = new ArrayList<>();
        objects.add(item1);
        objects.add(item2);
        objects.add(item3);
        setValues(objects,3000);
    }

    /**
     * 返回对应的样色
     * @param i
     * @return
     */
    public int getProgressColor(int i){
        switch (i){
            case 0:
                return POneColor;
            case 1:
                return PTwoColor;

            case 2:
                return PThreeColor;

        }

        return POneColor;
    }


    /**
     * dp转px
     */
    private int dp2px(float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, getContext().getResources().getDisplayMetrics());
    }


    /**
     * 设置背景圆环颜色
     *
     * @param color 背景圆环颜色
     */
    public void setBackColor(@ColorRes int color) {
        mBackPaint.setColor(ContextCompat.getColor(getContext(), color));
        invalidate();
    }

    /**
     * 设置进度圆环宽度
     *
     * @param width 进度圆环宽度
     */
    public void setProgWidth(int width) {
        mProgPaint.setStrokeWidth(width);
        invalidate();
    }
    float presont=0;
    List<ValueItem> list=new ArrayList<>();
    public void setValues(List<ValueItem> list,long animTime){
        this.list=list;
        if (animTime <= 0) {

        }
        else {
            ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                     presont = (float) animation.getAnimatedValue();
                    invalidate();
                }
            });
            animator.setInterpolator(new OvershootInterpolator());
            animator.setDuration(animTime);
            animator.start();
        }

    }



    /**
     * 设置进度圆环颜色(支持渐变色)
     *
     * @param startColor 进度圆环开始颜色
     * @param firstColor 进度圆环结束颜色
     */
    public void setProgColor(@ColorRes int startColor, @ColorRes int firstColor) {
        mColorArray = new int[]{ContextCompat.getColor(getContext(), startColor), ContextCompat.getColor(getContext(), firstColor)};
        mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR));
        invalidate();
    }

    /**
     * 设置进度圆环颜色(支持渐变色)
     *
     * @param colorArray 渐变色集合
     */
    public void setProgColor(@ColorRes int[] colorArray) {
        if (colorArray == null || colorArray.length < 2) return;
        mColorArray = new int[colorArray.length];
        for (int index = 0; index < colorArray.length; index++)
            mColorArray[index] = ContextCompat.getColor(getContext(), colorArray[index]);
        mProgPaint.setShader(new LinearGradient(0, 0, 0, getMeasuredWidth(), mColorArray, null, Shader.TileMode.MIRROR));
        invalidate();
    }

    public class ValueItem{
        String name;
        float values;

        public ValueItem(String name, float values) {
            this.name = name;
            this.values = values;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public float getValues() {
            return values;
        }

        public void setValues(float values) {
            this.values = values;
        }
    }
}